/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "foc_controller.h"
#include "pid.h"
#include "stdint.h"

/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
typedef enum
{
    SENSER_TLE5012B_TYPE,
    SENSER_AS5047P_TYPE,
    SENSER_ABZ_TYPE,
}senser_type_t;

typedef struct
{
    uint32_t crc32;
    
    uint8_t motor_pp;     //磁极对数
    uint8_t motor_dir;    //0:同向，1：反向
    senser_type_t senser_type;
    
    uint8_t reserv1;
    
    float rad_gain;
    float rad_offset;

}cfg_t;


typedef struct
{
    /* 严重故障，若电机处于工作状态，需要关闭 */
    union
    {
        struct
        {
            uint16_t motor_cfg_err    : 1;     /* 从FLASH中读取CFG参数错误 */
            uint16_t power_err        : 1;     /* 供电异常( 低于7V或者高于28V) */
            uint16_t can_comm_err     : 1;     /* CAN通讯错误 */
            uint16_t driver_temp_err  : 1;     /* 驱动温度异常（包括主控芯片及MOS管等） */
            uint16_t motor_temp_err   : 1;     /* 电机温度异常 */
        }bit;
        uint16_t all;
    }serious_fault;
    /* 轻度故障，若电机处于工作状态，可以维持工作 */
    union
    {
        struct
        {
            uint16_t flash_op_err  : 1;     /* FLASH读写错误 */
        }bit;
        uint16_t all;
    }mild_fault;
}fault_status_t;


typedef struct
{
    uint8_t curr_calib_finish_flag;
    /*三相电流采集偏置*/
    float ia_offset;
    float ib_offset;
    float ic_offset;
    
    float bus_voltage;
    float temp1;
    float temp2;
    
    uint8_t print_tag;
    
    uint32_t raw_angle;
    
    cfg_t cfg;

    fault_status_t fault_status;

    foc_type_t foc_type;
    
    uint16_t foc_start;
    uint16_t foc_study;
    
    uint64_t sys_tick;
    
    foc_controller_t foc_controller;
    
    pid_controller_t pid_uq;
    pid_controller_t pid_ud;
    pid_controller_t pid_speed;
    pid_controller_t pid_position;
}sys_t;

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;
extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_spi1_rx;
extern DMA_HandleTypeDef hdma_spi1_tx;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim7;
//extern TIM_HandleTypeDef htim4;
extern DMA_HandleTypeDef hdma_tim1_ch4;

extern uint64_t sys_time;
extern sys_t sys;
/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */
extern uint64_t get_sys_time_us(void);
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define LED_STA_Pin GPIO_PIN_3
#define LED_STA_GPIO_Port GPIOA
#define ID0_Pin GPIO_PIN_5
#define ID0_GPIO_Port GPIOA
#define ID1_Pin GPIO_PIN_6
#define ID1_GPIO_Port GPIOA
#define VM_AD_Pin GPIO_PIN_4
#define VM_AD_GPIO_Port GPIOC
#define TEMP2_AD_Pin GPIO_PIN_2
#define TEMP2_AD_GPIO_Port GPIOB
#define DRV_CAL_Pin GPIO_PIN_10
#define DRV_CAL_GPIO_Port GPIOB
#define TEMP1_AD_Pin GPIO_PIN_11
#define TEMP1_AD_GPIO_Port GPIOB
#define DRV_CS_Pin GPIO_PIN_12
#define DRV_CS_GPIO_Port GPIOB
#define DRV_SCLK_Pin GPIO_PIN_13
#define DRV_SCLK_GPIO_Port GPIOB
#define DRV_MISO_Pin GPIO_PIN_14
#define DRV_MISO_GPIO_Port GPIOB
#define DRV_MOSI_Pin GPIO_PIN_15
#define DRV_MOSI_GPIO_Port GPIOB
#define DRV_FAULT_Pin GPIO_PIN_6
#define DRV_FAULT_GPIO_Port GPIOC
#define CAN_STB_Pin GPIO_PIN_15
#define CAN_STB_GPIO_Port GPIOA
#define DRV_EN_Pin GPIO_PIN_10
#define DRV_EN_GPIO_Port GPIOC
#define CODER_CS_Pin GPIO_PIN_11
#define CODER_CS_GPIO_Port GPIOC
#define SPI1_SCLK_Z_Pin GPIO_PIN_3
#define SPI1_SCLK_Z_GPIO_Port GPIOB
#define SPI1_MISO_A_Pin GPIO_PIN_4
#define SPI1_MISO_A_GPIO_Port GPIOB
#define SPI1_MOSI_B_Pin GPIO_PIN_5
#define SPI1_MOSI_B_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
